package org.zjt.servletdemo;

import org.apache.catalina.Context;
import org.apache.catalina.connector.Connector;
import org.apache.coyote.http11.Http11Nio2Protocol;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.boot.context.embedded.tomcat.TomcatConnectorCustomizer;
import org.springframework.boot.context.embedded.tomcat.TomcatContextCustomizer;
import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
import org.springframework.context.annotation.Bean;
import com.alibaba.druid.support.http.StatViewServlet;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.context.annotation.EnableLoadTimeWeaving;
import org.springframework.context.annotation.LoadTimeWeavingConfigurer;
import org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver;
import org.springframework.instrument.classloading.LoadTimeWeaver;
import org.springframework.transaction.annotation.EnableTransactionManagement;


/**
 * -javaagent:F:\dev\repos\org\springframework\spring-instrument\4.3.10.RELEASE\spring-instrument-4.3.10.RELEASE.jar
 */
@EnableTransactionManagement
@SpringBootApplication
@EnableAspectJAutoProxy(/*proxyTargetClass=true*/)
@EnableLoadTimeWeaving
public class ServletDemoApplication implements LoadTimeWeavingConfigurer {

    @Override
    public LoadTimeWeaver getLoadTimeWeaver() {
        return new InstrumentationLoadTimeWeaver();
    }

    public static void main(String[] args) {
        SpringApplication.run(ServletDemoApplication.class, args);
    }


    /**
     * 配置druid数据源监控
     *
     * @return
     */
    @Bean
    public static ServletRegistrationBean servletRegistrationBean() {
        ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean();
        servletRegistrationBean.setServlet(new StatViewServlet());
        servletRegistrationBean.setName("druid");
        servletRegistrationBean.addUrlMappings("/druid/*");
        return servletRegistrationBean;
    }


    /**
     * servlet filter listen 的配置
     */
/*	@Bean
    public static FilterRegistrationBean filterRegistrationBean() {
		FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();

		filterRegistrationBean.setFilter(new MyFilter2());
		filterRegistrationBean.addServletNames("my-servlet2");

		filterRegistrationBean.setDispatcherTypes(DispatcherType.REQUEST, DispatcherType.FORWARD, DispatcherType.INCLUDE);

		return filterRegistrationBean;

	}

	@Bean
	public static ServletListenerRegistrationBean servletListenerRegistrationBean() {
		ServletListenerRegistrationBean servletListenerRegistrationBean = new ServletListenerRegistrationBean();
		servletListenerRegistrationBean.setListener(new MyServletRequestListener());
		return servletListenerRegistrationBean;
	}

	protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
		builder.sources(SegmentfaultLesson4Application.class);
		return builder;
	}*/


    /**
     * 自定义内嵌容器的方法
     * @return
     */
    @Bean
    public static EmbeddedServletContainerCustomizer embeddedServletContainerCustomizer() {

        //自定义内嵌容器
        return container -> {
            if (container instanceof TomcatEmbeddedServletContainerFactory) {

                //强制类型转换
                TomcatEmbeddedServletContainerFactory factory = TomcatEmbeddedServletContainerFactory.class.cast(container);

                //自定义应用URL的上下文
                factory.addContextCustomizers((TomcatContextCustomizer) context -> context.setPath("/spring-boot"));


                //自定义容器的端口和协议
                factory.addConnectorCustomizers((TomcatConnectorCustomizer) connector -> {
                    connector.setPort(8888);
                });
            }

        };
    }
}
